package com.zlyx.easy.log.loggers;

import java.util.Collection;
import java.util.Map;

import com.zlyx.easy.core.tool.Json;
import com.zlyx.easy.core.utils.DateUtils;
import com.zlyx.easy.core.utils.ExceptionUtils;
import com.zlyx.easy.core.utils.LogUtils;
import com.zlyx.easy.core.utils.ObjectUtils;

/**
 * @Auth 赵光
 * @Desc
 * @2018年12月13日 下午3:00:10
 */
public class Logger {

	private Class<?> cls;

	public Class<?> getCls() {
		return cls;
	}

	public void setCls(Class<?> cls) {
		this.cls = cls;
	}

	public Logger(Class<?> cls) {
		this.cls = cls;
	}

	/**
	 * 打印当前系统时间戳
	 * 
	 * @return
	 */
	public Object current() {
		return info(System.currentTimeMillis());
	}

	/**
	 * 打印当前系统时间
	 * 
	 * @return
	 */
	public Object date() {
		return info(DateUtils.getNow());
	}

	/**
	 * 打印当前系统时间(带毫秒)
	 * 
	 * @return
	 */
	public Object dateMs() {
		return info(DateUtils.getNowMs());
	}

	/**
	 * 打印单个对象
	 * 
	 * @param msg
	 * @return
	 */
	public Object info(Object msg) {
		LogUtils.info(cls, msg);
		return msg;
	}

	/**
	 * 打印键值对
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Object info(Object key, Object value) {
		if (ObjectUtils.isEmpty(key))
			return info(value);
		else
			return info(key + " -> " + value);
	}

	/**
	 * 打印key:多个对象
	 * 
	 * @param key
	 * @param objs
	 * @return
	 */
	public Object info(Object key, Object... objs) {
		return info(key, Json.pretty(objs));
	}

	/**
	 * 打印集合
	 * 
	 * @param c
	 * @return
	 */
	public Object info(Collection<?> c) {
		return info(Json.pretty(c));
	}

	/**
	 * 打印key:集合
	 * 
	 * @param key
	 * @param c
	 * @return
	 */
	public <T> Object info(Object key, Collection<?> c) {
		return info(key, Json.pretty(c));
	}

	/**
	 * 打印map
	 * 
	 * @param map
	 * @return
	 */
	public Object info(Map<?, ?> map) {
		return info(Json.pretty(map));
	}

	/**
	 * 打印key:map
	 * 
	 * @param key
	 * @param map
	 * @return
	 */
	public Object info(Object key, Map<?, ?> map) {
		return info(key, Json.pretty(map));
	}

	/**
	 * 打印警告消息
	 * 
	 * @param msg
	 * @return
	 */
	public Object warn(Object msg) {
		LogUtils.warn(cls, msg);
		return msg;
	}

	/**
	 * 打印debug消息
	 * 
	 * @param msg
	 * @return
	 */
	public Object debug(Object msg) {
		LogUtils.debug(cls, msg);
		return msg;
	}

	/**
	 * 打印err消息
	 * 
	 * @param declaringClass
	 * @param msg
	 * @return
	 */
	public Object err(Object msg) {
		LogUtils.err(cls, msg);
		return msg;
	}

	/**
	 * 打印异常堆栈消息(不会抛异常)
	 * 
	 * @param e
	 * @return
	 */
	public Object err(Throwable e) {
		return err(ExceptionUtils.getExceptionMsg(e));
	}

	public static Logger getLogger(Class<?> cls) {
		return new Logger(cls);
	}
}
